import { ConfigEnv, UserConfig, loadEnv } from 'vite'
import {createVitePlugins} from './vite/plugins'
import {createProxy} from './vite/proxy'
import {resolve} from 'path'
const root = process.cwd()
// https://vitejs.dev/config/

export default ({command, mode}: ConfigEnv): UserConfig => {
  const isBuild = command === "build"
  const env = loadEnv(mode, root);
  console.log('vite >>>', command, mode, env);
  return {
    resolve:{
      alias: [
        {
          find: /^@\//,
          replacement: resolve(root, './src')+'/'
        },{
          find: /@type/,
          replacement: resolve(root, './src/types')+'/'
        },{
          find: /@http/,
          replacement: resolve(root, './src/http')+'/'
        },{
          find: /^@store\//,
          replacement: resolve(root, './src/store/serve')+'/'
        }
      ]
    },
    css: {
      preprocessorOptions: {
        less: {
          javascriptEnabled: true
        }
      }
    },
    build: {
      target: 'es2015',
      brotliSize: false,
      assetsInlineLimit: 2000
    },
    plugins: createVitePlugins(isBuild, env),
    server:{
      proxy: createProxy(env)
    }
  }
}
